package com.corejava;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class ClassInitTest {

    public static void main(String[] args ) {
        (new Son()).thinking();
    }


}

class GrandFather {
    static {
        System.out.println("Grandfather is initiated!");
    }

    void thinking() {
        System.out.println("i am grandfather ");
    }
}

class Father extends GrandFather {
    @Override
    void thinking() {
        System.out.println("i am father");
    }
}

class Son extends Father {
    @Override
    void thinking() {
        try {
            MethodType methodType = MethodType.methodType(void.class);
            MethodHandle methodHandle = MethodHandles.lookup().findVirtual(GrandFather.class, "thinking", methodType).bindTo(new GrandFather());
            methodHandle.invokeExact();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}


